<?php

class ClienteController extends Zend_Controller_Action {

    public function init() {
        
    }
    
    
    public function showAction(){
        $id = $this->getParam('id');
        
        $tCliente = new DbTable_Cliente();
        $cliente = $tCliente->find($id)->current();
        
        $tBancos = new DbTable_Banco();
        $bancos = $tBancos->fetchAll(array('status_banco = (?)' => Status::DESBLOQUEADO));
        
        $tFilial = new DbTable_Filial();
        $filiais = $tFilial->fetchAll(array('status_filial = (?)' => Status::DESBLOQUEADO));
        
        $tEmprestimos = new DbTable_Emprestimo();
        $emprestimos = $tEmprestimos->fetchAll(array('Cliente_id = (?)' => $id));
        
        $session = new Zend_Session_Namespace('role');
       
        $this->view->session = $session;
        $this->view->emprestimos = $emprestimos;
        $this->view->bancos = $bancos;
        $this->view->filiais = $filiais;
        $this->view->cliente = $cliente;
    }
    

    public function excluirAction() {
        $tCliente = new DbTable_Cliente();
        $cliente = $tCliente->find($this->getParam('id'))->current();
        $cliente->delete();

        $this->redirect('/cliente/listar/');
    }

    public function listarAction() {
        $session = new Zend_Session_Namespace('role');
        $this->view->session = $session;
        
        $tCliente = new DbTable_Cliente();
        $param = $this->getParam('nome-cpf');
        $filtro = array();

        if (!is_null($this->getParam('msg')))
            $this->view->msg = Messages::getMessage($this->getParam('msg'));



        if (!is_null($param))
            $filtro = array("cpf like '%{$param}%' or nome like '%{$param}%'");

        $this->view->clientes = $tCliente->getByFilter($filtro);
    }

    public function ajaxEditarAction() {
        $this->_helper->viewRenderer->setNoRender();

        $tCliente = new DbTable_Cliente();
        $cliente = $tCliente->find($this->getParam('id'))->current();

        if ($cliente->cpf != $this->getParam('cpf')) {
            if ($tCliente->hasCpf($this->getParam('cpf'))) {
                $this->getResponse()->setHttpResponseCode(400);
                return;
            }
        }

        $cliente->nome = $this->getParam('nome');
        $cliente->cpf = $this->getParam('cpf');
        $cliente->save();

        $this->_helper->json(array('msg' => true));
    }

    public function ajaxCpfIncluidoAction() {
        $this->_helper->viewRenderer->setNoRender();

        $cpf = $this->getParam("cpf");
        $tCliente = new DbTable_Cliente();

        if ($tCliente->hasCpf($cpf))
            $this->getResponse()->setHttpResponseCode(400);
        else
            $this->_helper->json(array('msg' => true));
    }

    public function ajaxCadastraClienteAction() {
        $this->_helper->viewRenderer->setNoRender();

        $tCliente = new DbTable_Cliente();
        $cliente = $tCliente->createRow($this->getAllParams());

        try {
            $cliente->save();
            $this->_helper->json(array('msg' => true));
        } catch (Zend_Exception $e) {
            $this->getResponse()->setHttpResponseCode(400);
        }
    }

}
